package com.ycl.plus.lm.hardware.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ycl.plus.lm.common.idempotent.annotation.RepeatSubmit;
import com.ycl.plus.lm.common.log.annotation.Log;
import com.ycl.plus.lm.common.web.core.BaseController;
import com.ycl.plus.lm.common.mybatis.core.page.PageQuery;
import com.ycl.plus.lm.common.core.domain.R;
import com.ycl.plus.lm.common.core.validate.AddGroup;
import com.ycl.plus.lm.common.core.validate.EditGroup;
import com.ycl.plus.lm.common.log.enums.BusinessType;
import com.ycl.plus.lm.common.excel.utils.ExcelUtil;
import com.ycl.plus.lm.hardware.domain.vo.HardwareEquipSensorVo;
import com.ycl.plus.lm.hardware.domain.bo.HardwareEquipSensorBo;
import com.ycl.plus.lm.hardware.service.IHardwareEquipSensorService;
import com.ycl.plus.lm.common.mybatis.core.page.TableDataInfo;

/**
 * 设备和传感器关联
 * 前端访问路由地址为:/hardware/equipSensor
 *
 * @author devil-ycl
 * @date 2024-11-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/equipSensor")
public class HardwareEquipSensorController extends BaseController {

    private final IHardwareEquipSensorService hardwareEquipSensorService;

    /**
     * 查询设备和传感器关联列表
     */
    @SaCheckPermission("hardware:equipSensor:list")
    @GetMapping("/list")
    public TableDataInfo<HardwareEquipSensorVo> list(HardwareEquipSensorBo bo, PageQuery pageQuery) {
        return hardwareEquipSensorService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出设备和传感器关联列表
     */
    @SaCheckPermission("hardware:equipSensor:export")
    @Log(title = "设备和传感器关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HardwareEquipSensorBo bo, HttpServletResponse response) {
        List<HardwareEquipSensorVo> list = hardwareEquipSensorService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备和传感器关联", HardwareEquipSensorVo.class, response);
    }

    /**
     * 获取设备和传感器关联详细信息
     *
     * @param equipId 主键
     */
    @SaCheckPermission("hardware:equipSensor:query")
    @GetMapping("/{equipId}")
    public R<HardwareEquipSensorVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long equipId) {
        return R.ok(hardwareEquipSensorService.queryById(equipId));
    }

    /**
     * 新增设备和传感器关联
     */
    @SaCheckPermission("hardware:equipSensor:add")
    @Log(title = "设备和传感器关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HardwareEquipSensorBo bo) {
        return toAjax(hardwareEquipSensorService.insertByBo(bo));
    }

    /**
     * 修改设备和传感器关联
     */
    @SaCheckPermission("hardware:equipSensor:edit")
    @Log(title = "设备和传感器关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HardwareEquipSensorBo bo) {
        return toAjax(hardwareEquipSensorService.updateByBo(bo));
    }

    /**
     * 删除设备和传感器关联
     *
     * @param equipIds 主键串
     */
    @SaCheckPermission("hardware:equipSensor:remove")
    @Log(title = "设备和传感器关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{equipIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] equipIds) {
        return toAjax(hardwareEquipSensorService.deleteWithValidByIds(List.of(equipIds), true));
    }
}
